/*
 * XEventBusTest.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.eventbus;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.JButton;

import org.xfc.testing.XUITestCase;

public class XEventBusTest
	extends XUITestCase
{
	private XEventBus 	eventBus;
	private XSubscriber subscriber;
	
	private boolean		eventReceived;
	
	protected void setUp()
	{
		eventBus = new XEventBus();
		
		subscriber = new XSubscriber() {
			public void handleEvent(EventObject event) {
				eventReceived = true;
			}
		};
		
		eventReceived = false;
	}
	
	public void testSubscribeXSubscriberXEventFilter()
	{
		eventBus.subscribe(subscriber, MouseEvent.class);
		
		eventBus.publish(new MouseEvent(new JButton(), 0, 0, 0, 0, 0, 0, false));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(true));
		
		eventReceived = false;
		eventBus.unsubscribe(subscriber);
		
		eventBus.publish(new KeyEvent(new JButton(), 0, 0, 0, 0, 'a'));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(false));
	}

	public void testUnsubscribe()
	{
		eventBus.subscribe(subscriber);
		
		eventBus.publish(new MouseEvent(new JButton(), 0, 0, 0, 0, 0, 0, false));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(true));
		
		eventReceived = false;
		eventBus.unsubscribe(subscriber);
		
		eventBus.publish(new MouseEvent(new JButton(), 0, 0, 0, 0, 0, 0, false));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(false));
	}
	
	public void testPublish()
	{
		eventBus.subscribe(subscriber);
		
		eventBus.publish(new MouseEvent(new JButton(), 0, 0, 0, 0, 0, 0, false));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(true));
	}
	
	public void testProgressEvent()
	{
		eventBus.subscribe(subscriber, XProgressEvent.class);
		
		eventBus.publish(new XProgressEvent(new JButton(), XProgressEvent.INDETERMINATE_START));
		
		waitForIdle();
		
		assertThat(eventReceived, eq(true));
	}
	
	public void testStatusEvent()
	{
		eventBus.subscribe(subscriber, XStatusEvent.class);
		
		eventBus.publish(new XStatusEvent(new JButton(), "Starting download..."));
		
		waitForIdle();
		
		assertThat(true, eq(eventReceived));
		
		eventReceived = false;
		
		eventBus.publish(new XProgressEvent(new JButton(), XProgressEvent.INDETERMINATE_START));
		
		waitForIdle();
		
		assertThat(false, eq(eventReceived));
	}
	
}
